﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace Lipesin.BuildUpdateLaunch
{
	[Serializable]
	public class State
	{
		public DateTime LastWork { get; set; }
		private List<TimePerDay> _timeperday;
		public List<TimePerDay> TimePerDay { get { return _timeperday; } }

		public State()
		{
			LastWork = DateTime.Now;
			_timeperday = new List<TimePerDay>();
		}
		public State(Stream stream)
		{
			BinaryFormatter formatter = new BinaryFormatter();
			LastWork = (DateTime)formatter.Deserialize(stream);
			_timeperday = formatter.Deserialize(stream) as List<TimePerDay>;
		}
		public void Write(Stream stream)
		{
			BinaryFormatter formatter = new BinaryFormatter();
			formatter.Serialize(stream, LastWork);
			formatter.Serialize(stream, TimePerDay as List<TimePerDay>);
		}
		public void AddToToday(TimeSpan time)
		{
			TimePerDay today = TimePerDay.SingleOrDefault(item => item.Date == DateTime.Today);
			if (today == null)
			{
				today = new TimePerDay(DateTime.Today);
				TimePerDay.Add(today);
			}
			today.Time += time;
		}
	}

	[Serializable]
	public class TimePerDay
	{
		private DateTime _date;
		public DateTime Date { get { return _date; } }
		public TimeSpan Time { get; set; }

		public TimePerDay(DateTime date)
		{
			_date = date.Date;
			Time = new TimeSpan(0);
		}
	}
}
